<?php
$idMateria = $materia['Materia']['id'];
$idTipoParcial = $tiposParciales['0']['Tiposparciale']['id'];
?>
<div class="row-fluid">
   <div class="span12">
   <?php if (!empty($alumnos)): ?>
    <h3 class="heading">
        Materia "<?php echo $materia['Materia']['nombre'];  ?>"
        Curso: <?php echo $alumnos[0]['Paralelo']['curso']?>&nbsp;
        "<?php echo $alumnos[0]['Paralelo']['nombre']?>"
    </h3>
    <table class="table table-striped table-bordered table-condensed">
        <thead>
            <tr>
                <th rowspan="2">
                    Nro
                </th>
                <th rowspan="2">
                    Alumnos
                </th>
                   
                    <?php foreach($tiposParciales as $tipo): ?>
                    <?php $c=0; ?>
                        <?php foreach($tipo['Parciale'] as $parcial): ?>
                          <?php 
                          $c++;
                          ?>
                       <?php endforeach; ?>
                       
                       <?php if(!empty($tipo['Parciale'])): ?>
                          
                          <th colspan="<?php echo $c;?>">
                          <?php echo $tipo['Tiposparciale']['nombre']?>
                          </th>
                       <?php endif; ?>
                       
                    <?php endforeach; ?>
                <th rowspan="2">
                    Promedio
                </th>
                <th rowspan="2">
                    DPS
                </th>
             </tr>
             <tr>  
                   
                   <?php foreach($tiposParciales as $tipo): ?>
                       <?php foreach($tipo['Parciale'] as $parcial): ?>
                       <?php $cantidad++; ?>
                       <th>
                          
                          <?php echo $parcial['nombre']?>
                       </th>
                       <?php endforeach; ?>
                    <?php endforeach; ?>
                                                                                           
                
            </tr>
        </thead>
        <tbody>
            
            <?php 
            $count=1;
            echo $this->Form->create('Notas'); ?>
            
            <?php foreach ($alumnos as $a): ?>
            <?php $idAlumno = $a['Alumno']['id']; ?>
                <tr>
                    <td>
                    <?php echo $count; $count++ ?>
                    </td>
                    <td>
                        <?php echo $a['Alumno']['ap_paterno']; ?>&nbsp;
                        <?php echo $a['Alumno']['ap_materno']; ?>&nbsp;
                        <?php echo $a['Alumno']['nombre']; ?>
                    </td>
                    <?php $notaId=0; ?>
                    <?php foreach($tiposParciales as $tipo): ?>
                    
                       <?php foreach($tipo['Parciale'] as $parcial): ?>
                       <td>
                        <?php 
                        echo $this->Form->hidden("$idAlumno.$notaId.alumno_id", array('value'=>$idAlumno));
                        echo $this->Form->hidden("$idAlumno.$notaId.tipoparciale_id", array('value'=>$parcial['tiposparciale_id']));
                        echo $this->Form->hidden("$idAlumno.$notaId.parciale_id", array('value'=>$parcial['id']));
                        echo $this->Form->hidden("$idAlumno.$notaId.profesore_id", array('value'=>$idProfesor));
                        //echo $this->Form->hidden("$idAlumno.AlumnosParciale.ciclo_id", array('value'=>$ciclo));
                        echo $this->Form->hidden("$idAlumno.$notaId.materia_id", array('value'=>$idMateria));
                        echo $this->Form->hidden("$idAlumno.$notaId.promedio", array('value'=>0));
                        echo $this->Form->hidden("$idAlumno.$notaId.dps", array('value'=>0));
                        echo $this->Form->hidden("$idAlumno.$notaId.gestion", array('value'=>$gestion));
                        $id = 'nota_'.$idAlumno.'_'.$notaId;
                        echo $this->Form->text("$idAlumno.$notaId.nota", array('placeholder' => 0, 'class' => 'input-mini', 'required', 'id'=>"$id"));
                        ?>
                    
                       </td>
                    <?php
                    $notaId++;
                       endforeach; ?>
                    <?php endforeach; ?>
                    <td>
                    <?php
                        echo $this->Form->hidden("$idAlumno.$notaId.alumno_id", array('value'=>$idAlumno));
                        echo $this->Form->hidden("$idAlumno.$notaId.profesore_id", array('value'=>$idProfesor));
                        //echo $this->Form->hidden("$idAlumno.AlumnosParciale.ciclo_id", array('value'=>$ciclo));
                        echo $this->Form->hidden("$idAlumno.$notaId.materia_id", array('value'=>$idMateria));
                        echo $this->Form->hidden("$idAlumno.$notaId.gestion", array('value'=>$gestion));
                        $idPromedio = 'promedio_'.$idAlumno;
                    echo $this->Form->hidden("$idAlumno.$notaId.promedio", array('id'=>$idPromedio));
                    echo $this->Form->text("$idAlumno.total", array(
                                        'placeholder' => 0,
                                        'class' => 'input-mini',
                                       "disabled" => "disabled"));
                    ?>
                    </td>
                    <td>
                    <?php echo $this->Form->text("$idAlumno.$notaId.dps", array('placeholder' => 0, 'class' => 'input-mini', 'required'));?>
                    </td>
            
                        <script>
                        function redondeo2decimales(numero)
                        {
                        	var original = parseFloat(numero);
                        	var result = Math.round(original*100)/100 ;
                        	return result;
                        }				
                            $(document).ready(function() {
                                $("input[type='text']").change(function(){
                                    var suma = 0;
                                  
                                    <?php for($i=0; $i < $cantidad; $i++): ?>
                                        suma += Number($("input[id='nota_<?php echo $idAlumno ?>_<?php echo $i;?>']").val());
                                        
                                    <?php endfor;  ?>
                                     
                                    console.log('la variable es'+suma); 
                                    total = suma / <?php echo $cantidad; ?>; 
                                    rtotal = redondeo2decimales(total);
                                    $("#Notas<?php echo $idAlumno; ?>Total").val(rtotal); 
                                    $("#promedio_<?php echo $idAlumno; ?>").val(rtotal);
                                    //suma
                                });
                            });				
                        </script>
                </tr>
    <?php endforeach; ?>
        </tbody>
    </table>
    <div class="form-actions">
       <?php $option = array('name'=>'Registrar','Value'=>'Registrar','class'=>'btn btn-success') ?>
       <?php //echo $this->Form->end($option); ?>
        <button class="btn btn-success" type="submit">
            Registrar
        </button>
    </div>
    </form>
<?php else: ?>
    <h3 class="heading">
        No tiene Alumnos
    </h3>
<?php endif; ?>	
   
   </div>
</div>
    
<!-- sidebar -->
<?php echo $this->element('sidebar/profesor'); ?>
<!-- fin sidebar -->			